from toee import *
from py00439script_daemon import record_time_stamp, get_v, set_v, tsc, within_rect_by_corners

def san_dialog( attachee, triggerer ):
	record_time_stamp(517)
	triggerer.turn_towards(attachee)			## added by Livonya
	attachee.turn_towards(triggerer)			## added by Livonya
	if ( anyone(triggerer.group_list(),"has_follower",8031) ):
	# Prince Thrommel in your party
			triggerer.begin_dialog( attachee, 700 )
	elif ( anyone(triggerer.group_list(),"has_follower",8040) and (game.global_flags[192] == 0) ):
	# Ashrem in your party
		triggerer.begin_dialog( attachee, 860 )
	elif ( (game.global_flags[115] == 1) and (game.global_flags[116] == 1) and (game.global_flags[125] == 0) ):
	# Tubal and Antonio are dead (116 & 115 respectively), and you haven't bluffed him yet (125)
		triggerer.begin_dialog( attachee, 400 )
	elif (not attachee.has_met(triggerer)):
		if (game.global_flags[92] == 1):
		# Recruited via Wat
			triggerer.begin_dialog( attachee, 200 )
		else:
		# Waltzing In (TM)
			triggerer.begin_dialog( attachee, 1 )
	else:
		# "What news have you for me"
		triggerer.begin_dialog( attachee, 300 )

def san_dying( attachee, triggerer ):
	game.global_flags[107] = 1
	record_time_stamp(459)
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[107] = 0
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	game.global_flags[344] = 0
	return RUN_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		game.global_vars[713] = 0
	if (game.global_flags[312] == 1):
		attachee.object_flag_set(OF_OFF)
		game.global_flags[107] = 1
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	#if (attachee.map == 5067 and game.global_flags[872] == 0):
	#	# spawn fire chest (obsolete - now Mobbed in via World Builder)
	#	chest = game.obj_create( 1012, location_from_axis (409L, 498L))
	#	chest.rotation = 4
	#	create_item_in_inventory( 6311, chest )
	#	create_item_in_inventory( 6197, chest )
	#	create_item_in_inventory( 6186, chest )
	#	create_item_in_inventory( 12037, chest )
	#	game.global_flags[872] = 1
	if (not game.combat_is_active()):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (not attachee.has_met(obj)):
				if (is_safe_to_talk(attachee,obj)):
					record_time_stamp(517)
					if ( anyone(triggerer.group_list(),"has_follower",8031) ):
					# Thrommel in Party
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 700 )
					elif ( anyone(triggerer.group_list(),"has_follower",8040) and (game.global_flags[192] == 0) ):
					# Ashrem in Party
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 860 )
					elif ( (game.global_flags[104] == 1) or (game.global_flags[105] == 1) or (game.global_flags[106] == 1) ):
					# Killed one of the other priests
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 730 )
					elif (game.global_flags[92] == 1):
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 200 )
					else:
					# Recruited by Wat
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog(attachee,1)
						#game.new_sid = 0			## removed by Livonya
	if (game.global_vars[713] == 0 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_resist_elements, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[713] == 4 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_protection_from_elements, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[713] == 8 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_shield_of_faith, attachee)
		attachee.spells_pending_to_memorized()
	game.global_vars[713] = game.global_vars[713] + 1
	return RUN_DEFAULT

def escort_below( attachee, triggerer ):
	# game.global_flags[144] = 1
	game.global_vars[691] = 3
	game.fade_and_teleport(0,0,0,5080,478,451)
	return RUN_DEFAULT

def talk_Ashrem( attachee, triggerer, line):
	ashrem = find_npc_near(attachee,8040)
	if (ashrem != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(ashrem,line)
		ashrem.turn_towards(attachee)
		attachee.turn_towards(ashrem)
	return SKIP_DEFAULT